<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-15
 * Time: 22:51
 */
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\controller\Upload;
use app\common\extend\Wxreturn;
use app\common\model\WxMedia as cxModel;
use app\common\model\WxMediacontent;
use think\Loader;
use weixinhack\WxBase;

class WxMedia extends Adminbase {
    protected $beforeActionList = [
        'btnyz'  =>  ['only'=>'index,add,edit'],
    ];
    protected function btnyz(){
        $btn = $this->btnauth('add,edit,del');
        $this->assign('btn',$btn);
    }
    public function index($types = 'news'){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        if(!empty($getdata['type'])){
            $types = $getdata['type'];
        }
        $listdata = $cxmodel->where('type',$types)->paginate('20',false,['query' => request()->param()])->each(function ($item){
            $item->cont = model('WxMediacontent')->where('media_id',$item->media_id)->select();
            $item->num = model('WxMediacontent')->where('media_id',$item->media_id)->count();
        });
        $this->assign([
            'list' => $listdata,
        ]);
        return view();
    }
    public function savemedia(){
        $wxmodel = new WxBase();
        $wxtoken = $wxmodel->access_token($this->webdb['wxappid'],$this->webdb['wxappsecret']);
        $mediacont = $wxmodel->get_mediacont($wxtoken);
        $mediacont = json_decode($mediacont,true);
        foreach ($mediacont as $k => $v){
            if($k == 'voice_count' && $v != '0'){
                $medialist['voice'] = $this->getMedialist('voice',$v);
            }
            if($k == 'video_count' && $v != '0'){
                $medialist['video'] = $this->getMedialist('video',$v);
            }
            if($k == 'image_count' && $v != '0'){
                $medialist['image'] = $this->getMedialist('image',$v);
            }
            if($k == 'news_count' && $v != '0'){
                $medialist['news'] = $this->getMedialist('news',$v);
            }
        }
        $this->success("更新成功！");
        return true;
    }
    protected function getMedialist($cl,$total_count,$count = '0'){
        $cxmodel = new cxModel();
        $contmodel = new WxMediacontent();
        $wxmodel = new WxBase();
        $upmodel = new Upload();
        $token = $wxmodel->access_token($this->webdb['wxappid'],$this->webdb['wxappsecret']);
        $data = array(
            "type" => $cl,
            "offset" => $count,
            "count" => '20'
        );
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $array = $wxmodel->get_media($data,$token);
        $array = json_decode($array,true);
        foreach ($array['item'] as $k => $v){
            $v['wxid'] = $this->webdb['wxid'];
            $v['type'] = $cl;
            $olddata = $cxmodel->where('media_id',$v['media_id'])->value('id');
            if($cl == 'news'){
                $itcont = $v['content'];
                unset($v['content']);
                foreach ($itcont['news_item'] as $v1){
                    $oldcont = $contmodel->where('thumb_media_id',$v1['thumb_media_id'])->value('id');
                    $v1['media_id'] = $v['media_id'];
                    if(empty($v1['thumb_url'])){
                        $v1['thumb_url'] = $upmodel->fileMove($v1['thumb_url'],'weixin','copy',false);
                    }
                    if(empty($oldcont)){
                        WxMediacontent::create($v1);
                    }else{
                        $contmodel->allowField(true)->isUpdate(true)->save($v1,['id'=>$oldcont]);
                    }
                }
            }
            if($cl == 'image' && empty($v['url'])){
                $v['url'] = $upmodel->fileMove($v['url'],'weixin','copy',false);
            }
            if(empty($olddata) && !isset($olddata)){
                cxModel::create($v);
            }else{
                $cxmodel->allowField(true)->isUpdate(true)->save($v,['id'=>$olddata]);
            }
        }
        $count = $count + $array['item_count'];
        if($array['total_count'] <= $count){
            return true;
        }else{
            $this->getMedialist($cl,$array['total_count'],$count);
        }
    }

}